package com.midea.ai.air.ui.device.product.utils;

import android.content.Context;

public class MetricsUtils
{

	public static int Dp2Px(Context context, float dp)
	{
		try
		{
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (dp * scale + 0.5f);
		}
		catch (Exception e)
		{
			return (int) (dp * 2 + 0.5f);
		}
	}

	public static int Px2Dp(Context context, float px)
	{
		try
		{
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (px / scale + 0.5f);
		}
		catch (Exception e)
		{
			return (int) (px / 2 + 0.5f);
		}
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 * 
	 * @param pxValue
	 * @return
	 */
	public static int px2sp(Context context, float pxValue)
	{
		try
		{
			final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
			return (int) (pxValue / fontScale + 0.5f);
		}
		catch (Exception e)
		{
			return (int) (pxValue + 0.5f);
		}
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @return
	 */
	public static int sp2px(Context context, float spValue)
	{
		try
		{
			final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
			return (int) (spValue * fontScale + 0.5f);
		}
		catch (Exception e)
		{
			return (int) (spValue * 1 + 0.5f);
		}
	}

}
